import { ref, onMounted } from "vue"
import { getSubcategory } from "@/api/category.js"
import { onBeforeRouteUpdate, useRoute } from "vue-router"

export const useCategory = () => {
  const route = useRoute()
  const data = ref({})
  const getSubList = async (id=route.params.id) => {
    const res = await getSubcategory(id)
    data.value = res.result
    // console.log(res)
    // console.log(cateName.value)
  }

  onMounted(() => { getSubList() })

  onBeforeRouteUpdate( (to) => {
    getSubList(to.params.id)
  })

  return {
    getSubList,
    data
  }

}
